<?php

class Elastic_Customizer {
	/**
	 * Customizes files for the elastic theme
	 *
	 * @return void
	 * @author Daryl Koopersmith
	 * 
	 */
	function run() {
		$html = $_POST["html"];
		$css = $_POST["css"];
		//$json = stripslashes( $_POST["json"] );
		$json = json_decode( stripslashes( $_POST["json"] ) , true);
		$state = stripslashes( $_POST["state"] );
		$settings = json_decode( stripslashes( $_POST["settings"] ) );


		$layout = $this->generate_layout( $html );
		$style = $this->generate_style( $settings );

		$files = array(
			"layout.php" => $layout,
			"style.css" => $style,
			"structure.css" => $css,
			//"json.php" => $json
			"json.php" => var_export( get_themes(), true ),
			"state.php" => $state
			);


		return $files;
	}
	
	
	/**
	 * Extracts data from ids in formatted html. Will only extract data from divs.
	 * Ids are processed in the form of type_name or type_name_parent-type_parent-name.
	 *
	 * @param string $html 
	 * @return array Extracted id data
	 * @author Daryl Koopersmith
	 */
	function extract_ids( $html ) {
		// This works for isolating IDs generally
		//preg_match_all( "/\<div.*?id=\"(.*?)\".*?\>/s", $text, $divs, PREG_SET_ORDER );

		// Sometimes we get escaped " characters.
		$html = stripslashes($html);

		// This works for matching arguments WITHOUT accounting for escaped "s
		preg_match_all( "/\<div.*?id=\"(?P<type>.*?)_(?P<name>.*?)(?:_(?P<parent_type>.*?)_(?P<parent_name>.*?)(?:_.*?)?)?\".*?\>/s", $html, $ids, PREG_SET_ORDER );

		return $ids;
	}
	
	/**
	 * Dynamically generates the contents of style.css
	 *
	 * @param string $settings Theme settings
	 * @return string A valid css file
	 * @author Daryl Koopersmith
	 */
	function generate_style( $settings ) {
		$out = "/*\nTheme Name: ";
		$out.= $settings->name;
		$out.= "\nTheme URI: ";
		$out.= $settings->uri;
		$out.= "\nDescription: ";
		$out.= $settings->description;
		$out.= "\nVersion: ";
		$out.= $settings->version;
		$out.= "\nAuthor: ";
		$out.= $settings->author;
		$out.= "\nAuthor URI: ";
		$out.= $settings->author_uri;
		$out.= "\nTags: ";
		$out.= $settings->tags;
		$out.= "\n*/\n\n";
		
		ob_start();
		include("framework_style.css");
		$out.= ob_get_clean();
		
		return $out;
	}
	
	/**
	 * Dynamically generates the contents of layout.php
	 *
	 * @param string $html 
	 * @return string A valid php file
	 * @author Daryl Koopersmith
	 */
	function generate_layout($html) {
		$divs = $this->extract_ids( $html );
		
		if ( ! empty($divs) ) {
			$out = '<?php' . "\n" . '$layout = ';
			$breadcrumb = array();

			for($i = 0; $i < count($divs); $i++) {
				$type = $divs[$i]['type'];
				$name = $divs[$i]['name'];
				$next_parent_type = $divs[$i+1]['parent_type'];
				$next_parent_name = $divs[$i+1]['parent_name'];

				// Format text
				if( $i != 0 ) {
					$out .= "\n";
					foreach($breadcrumb as $ignore) {
						$out .= "\t";
					}
				}

				$out .= 'new ' . ucfirst($type) . '("' . $name . '"';

				// If divs[i] is a parent, add the array markup.
				if($type == $next_parent_type && $name == $next_parent_name) {
					$out .= ', array(';
					array_unshift($breadcrumb, array("type" => $type, "name" => $name));
				} else {
					$out .= ')';
					for($j = 0; $j < count($breadcrumb) + 1; $j++) {
						// If there are no more parents, or if the next_parent is breadcrumb[$j]
						if ($j == count($breadcrumb) || ($next_parent_type == $breadcrumb[$j]['type'] && $next_parent_name == $breadcrumb[$j]['name'])) {
							for ($k = 0; $k < $j; $k++) {
								array_shift($breadcrumb);

								// Format text
								$out .= "\n";
								foreach($breadcrumb as $ignore) {
									$out .= "\t";
								}

								$out .= '))';
							}
							if (count($breadcrumb) == 0)
								$out .= ';';
							else
								$out .= ',';

							break;
						}
					}
				}
			}

			$out .= "\n" . '$layout->run();' . "\n" . '?>';
			
			return $out;
		} else {
			return false;
		}
	}
}

?>